import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { Village } from '../entity/Village';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { EventManager } from '../event/EventManager';
import { Soldier } from '../entity/Soldier';
import { ArmySoldier } from './ArmySoldier';
import { Army, ArmyType, ArmyTypeValue } from '../entity/Army';
import ArmyApi from '../api/ArmyApi';
import { ComfirmArmy } from './ComfirmArmy';
import { UserInfo } from '../common/UserInfo';
const { ccclass, property } = _decorator;

@ccclass('BattleLayout')
export class BattleLayout extends Component {
    @property(Label)
    villageName: Label
    @property(Label)
    coord: Label
    @property(Node)
    target: Node
    @property(Node)
    help: Node
    @property(Node)
    look: Node
    @property(Node)
    attack: Node
    @property(Node)
    rob: Node
    @property(Node)
    soldierContent: Node
    @property(Node)
    submit: Node
    @property(Prefab)
    armySoldier: Prefab
    @property(SpriteFrame)
    btnSpriteFrame1: SpriteFrame
    @property(SpriteFrame)
    btnSpriteFrame2: SpriteFrame
    private _village: Village


    private _army: Army = new Army()

    start() {
        this.target.on(Node.EventType.TOUCH_END, this.onTarget, this)
        this.help.on(Node.EventType.TOUCH_END, this.onHelp, this)
        this.look.on(Node.EventType.TOUCH_END, this.onLook, this)
        this.attack.on(Node.EventType.TOUCH_END, this.onAttack, this)
        this.rob.on(Node.EventType.TOUCH_END, this.onRob, this)
        this.submit.on(Node.EventType.TOUCH_END, this.onSubmit, this)

        EventManager.instance.on(EventManager.EventType.TargetVillage, this.onTargetVillage, this)
        EventManager.instance.on(EventManager.EventType.ArmySetOff, this.onArmySetOff, this)

        this.init()
    }

    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.TargetVillage, this.onTargetVillage, this)
        EventManager.instance.on(EventManager.EventType.ArmySetOff, this.onArmySetOff, this)
    }

    async init() {

        // this._village = null
        if (!this._village) {
            this.villageName.string = '请选择一个目标'
            this.coord.string = ''
        }


        let soldierList = Soldier.createBySoldiersString()
        for (let item of soldierList) {
            let node = instantiate(this.armySoldier)
            this.soldierContent.addChild(node)
            let as = node.getComponent(ArmySoldier)
            as.setData(item)
          
        }

        this.onHelp()
    }

    onTarget() {
        UIManager.instance.openUI(PrefabEnum.SelectCoord)
    }
    onHelp() {
        this.setActive(this.help)
        this._army.armyType = ArmyType.Help
        this.showAllSoldier()
    }
    onLook() {
        this.setActive(this.look)
        this._army.armyType = ArmyType.Detection
        this.showScoutSoldier()
    }
    onAttack() {
        this.setActive(this.attack)
        this._army.armyType = ArmyType.Attack
        this.showFightSoldier()
    }
    onRob() {
        this.setActive(this.rob)
        this._army.armyType = ArmyType.Rob
        this.showFightSoldier()
    }
    showAllSoldier() {
        let children = this.soldierContent.children
        for (let ch of children) {
            let as = ch.getComponent(ArmySoldier)
            as.setEnable(true)
        }
    }
    showFightSoldier() {
        let children = this.soldierContent.children
        for (let ch of children) {
            let armySoldier = ch.getComponent(ArmySoldier)
            if (armySoldier.isScout) {
               
                armySoldier.setEnable(false)
            } else {
               
                armySoldier.setEnable(true)
            }
        }
    }
    showScoutSoldier() {
        let children = this.soldierContent.children
        for (let ch of children) {
            let armySoldier = ch.getComponent(ArmySoldier)
            if (armySoldier.isScout) {
                armySoldier.setEnable(true)
            } else {
                armySoldier.setEnable(false)
            }
        }
    }
    async onSubmit() {

        if (!this._village) {
            UIManager.instance.tip('请选择目标村庄')
            return
        }

        if (!this._army.armyType) {
            UIManager.instance.tip('请选择出征类型')
            return
        }
        if(!this._village.isOverThreeDay){
            UIManager.instance.modal('提示','该玩家处在新手保护期', false)
            return
        }
        

        this._army.soldierList = []
        let children = this.soldierContent.children
        for (let ch of children) {
            let armySoldier = ch.getComponent(ArmySoldier).getArmySoldier()
            this._army.soldierList.push(armySoldier)
        }


        if (this._army.soldierCount == 0) {
            UIManager.instance.tip('请选择出征士兵')
            return
        }
        let params = {
            x: this._village.x,
            y: this._village.y,
            ...this._army.params
        }



        console.log('出兵验证：', params)
        await UIManager.instance.showLoading()
        let res = await ArmyApi.a2bOper(params)
        UIManager.instance.closeLoading()
        console.log(res)

        let ui = await UIManager.instance.openUI(PrefabEnum.ComfirmArmy)
        ui.getComponent(ComfirmArmy).setData(this._village, this._army)
    }

    async onArmySetOff() {

    }

    setActive(btn: Node) {
        this.help.getComponent(Sprite).spriteFrame = this.btnSpriteFrame2
        this.look.getComponent(Sprite).spriteFrame = this.btnSpriteFrame2
        this.rob.getComponent(Sprite).spriteFrame = this.btnSpriteFrame2
        this.attack.getComponent(Sprite).spriteFrame = this.btnSpriteFrame2

        btn.getComponent(Sprite).spriteFrame = this.btnSpriteFrame1
    }

    onTargetVillage(village: Village) {
        
        this._village = village
        this.villageName.string = village.name
        this.coord.string = `( ${village.x} | ${village.y} )`

        if(village.user.allyId == UserInfo.instance.villageData.user.allyId){
            this.villageName.string = village.name + ' (盟友)'
            this.look.active = false
            this.attack.active = false
            this.rob.active = false
        } else {
            this.villageName.string = village.name
            this.look.active = true
            this.attack.active = true
            this.rob.active = true
        }
    }

}


